package com.yanchangapp.ui.activity.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import com.yanchangapp.BuildConfig;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.Version;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.yanchangapp.R;
import com.yanchangapp.factory.HomeMainFactory;
import com.yanchangapp.ui.fragment.home.HomeFragment;
import com.yanchangapp.ui.fragment.home.MeFragment;
import com.yanchangapp.ui.fragment.home.WorkFragment;
import com.zxl.baselib.ui.base.ExceptionHandle;

import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * @author zxl on 2018/06/30.
 * discription: 首页
 */

public class MainActivity extends BaseActivity {
    @BindView(R.id.fg_main)
    FrameLayout fgMain;
    @BindView(R.id.borderLine)
    View borderLine;
    @BindView(R.id.rbHome)
    RadioButton rbHome;
    @BindView(R.id.rbWork)
    RadioButton rbWork;
    @BindView(R.id.rbMe)
    RadioButton rbMe;
    @BindView(R.id.rpMain)
    RadioGroup rpMain;
    private FragmentManager mSupportFragmentManager;

    private HomeFragment mHomeFragment;
    private WorkFragment mWorkFragment;
    private MeFragment mMeFragment;
    private boolean firstIn = true;
    @Override
    protected void init() {
        mHomeFragment = null;
        mWorkFragment = null;
        mMeFragment = null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        clearOldFragment();
        showWhichFragment(0);
        rbHome.setChecked(true);
    }

    @Override
    protected void initData() {
        checkUpdate();
    }

    private void checkUpdate() {
        HeadApiRetrofit.getInstance().checkVersionUpdate()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<Version>>() {
                    @Override
                    public void onNext(BaseResponse<Version> versionBaseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == versionBaseResponse.getCode()){
                            Version version = versionBaseResponse.getData();
                            int currentVersionCode = BuildConfig.VERSION_CODE;
                            if (currentVersionCode < version.getVersionCode()){
                                // 进行更新
                                UpdateActivity.show(MainActivity.this,version);
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {

                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (firstIn){
            firstIn = false;
        }else {
            if (mHomeFragment != null){
                mHomeFragment.homeShow();
            }
        }
        Log.e("轮播图...主页", "onResume: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mHomeFragment != null){
            mHomeFragment.homeHidden();
        }
        Log.e("轮播图...主页", "onStop: ");
    }

    @Override
    protected void initListener() {
        rpMain.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rbHome:
                    showWhichFragment(0);
                    break;
                case R.id.rbWork:
                    showWhichFragment(1);
                    break;
                case R.id.rbMe:
                    showWhichFragment(2);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("全局", "MainActivity回收了");
    }

    private void  showWhichFragment(int index){
        FragmentTransaction fragmentTransaction = mSupportFragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (index){
            case 0:
                if (mHomeFragment == null){
                    mHomeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.fg_main,mHomeFragment);
                }else {
                    fragmentTransaction.show(mHomeFragment);
                }
                break;
            case 1:
                if (mWorkFragment == null){
                    mWorkFragment = new WorkFragment();
                    fragmentTransaction.add(R.id.fg_main,mWorkFragment);
                }else {
                    fragmentTransaction.show(mWorkFragment);
                }
                break;
            case 2:
                if (mMeFragment == null){
                    mMeFragment = new MeFragment();
                    fragmentTransaction.add(R.id.fg_main,mMeFragment);
                }else {
                    fragmentTransaction.show(mMeFragment);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    private void  hideAllFragment(FragmentTransaction fragmentTransaction){
        if (mHomeFragment!= null){
            fragmentTransaction.hide(mHomeFragment);
        }

        if (mWorkFragment != null){
            fragmentTransaction.hide(mWorkFragment);
        }

        if (mMeFragment != null){
            fragmentTransaction.hide(mMeFragment);
        }
    }

    // 清除老的Fragment
    private void clearOldFragment() {
        if (mSupportFragmentManager == null){
            mSupportFragmentManager = getSupportFragmentManager();
        }
        FragmentTransaction transaction = mSupportFragmentManager.beginTransaction();
        List<Fragment> fragments = mSupportFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0) {
            return;
        }
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit) {
            transaction.commitNow();
        }
    }
}
